package com.os.crm.service;

import com.os.crm.pojo.Enterprise;
import com.os.sys.service.bean.UserInfo;
import com.os.util.BPConfiguration;
import com.os.util.BPConstants;
import com.os.util.Tools;
import lombok.extern.slf4j.Slf4j;
import net.sf.jxls.util.Util;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.FileService;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class POIService {

    @Autowired
    FileService fileService;
    private static String getTemplateName() {
        String base = "enterprise/";
        return base;
    }

    private static String getTimeStr() {
        return Tools.getTimeMillStr();
    }

    /**
     * 导出区县上报数据列出
     *
     * @param userinfo
     * @param base
     * @param enterpriseList
     * @param map
     * @return
     */
    public String genExcel4EnterpriseList(UserInfo userinfo, String base, List<Enterprise> enterpriseList,
                                                 Map map) {
        // Project project =
        // DaoFactory.getProjectDao().selectProjectInfoById(pro_id);
        // 复制模版,生成临时文件,写入临时文件,然后把临时文件地址返回
        String path_base = BPConfiguration.getPathBase();
        String dir = path_base + "/" + getTemplateName() + "enterprise_list.xlsx";// 表单模板
        // String dirtemp =
        // base+"/"+getTemplateName()+"project_apr_template.xlsx";//表单模板
        String dir_temp = base + "/" + getTemplateName() + "/tmplword/";

        // String fileName=templateName +".xlsx";
        // 模版完整路径
        // String filePath= dir + fileName;
        String fileNameNew = "enterprise" + "_" + getTimeStr() + ".xlsx";
        // String fileNameNewTemp = "中心评审流程单Temp"+"_"+getTimeStr()+".xlsx";
        String filePathNew = dir_temp + fileNameNew;
        // String filePathNewTemp = dir_temp + fileNameNewTemp ;

        fileService.copyFile(dir, filePathNew);
        // fileBO.copyFile(dirtemp, filePathNewTemp);
        File fileTemp = new File(dir_temp);
        if (!fileTemp.exists()) {
            fileTemp.mkdirs();
        }
        int row = 1;
        int column = 1;
        String content = "内容";
        boolean flag = false;
        FileOutputStream out = null;
        XSSFWorkbook xwb;
        try {
            // 目标
            xwb = new XSSFWorkbook(new FileInputStream(filePathNew));
            XSSFSheet xSheet = xwb.getSheetAt(0);
            // 模板
            XSSFSheet xSheetTemp = xwb.getSheetAt(1);

            int number = 1;
            int number1 = 10;
            XSSFRow sourceRow = null;
            XSSFRow targetRow = null;
            // 生成table 初审
            for (int i = 0; i < enterpriseList.size(); i++) {
                Enterprise enterprise = enterpriseList.get(i);
                sourceRow = xSheetTemp.getRow(1);
                targetRow = xSheet.createRow(number);
                // 复制前把所有当前内容下移一行
                Util.copyRow(xSheet, sourceRow, targetRow);
                int index = 0;
                setCellValue(xSheet, number, index++, (i+1));// 序号
                setCellValue(xSheet, number, index++, enterprise.getEnt_name());// 单位
                setCellValue(xSheet, number, index++, enterprise.getPhone());// 联系电话
                setCellValue(xSheet, number, index++, enterprise.getAddress());// 地址
                setCellValue(xSheet, number, index++, enterprise.getBusiness_name());//行业
                setCellValue(xSheet, number, index++, enterprise.getProvince_name());//省
                setCellValue(xSheet, number, index++, enterprise.getCity_name());//市
                setCellValue(xSheet, number, index++, enterprise.getArea_name());//区县
                setCellValue(xSheet, number, index++, enterprise.getEnt_comment() );// 备注
                setCellValue(xSheet, number, index++, enterprise.getCreate_time() + "");//创建时间
                setCellValue(xSheet, number, index++, enterprise.getUpdate_time()+ "");//更新时间

                number++;
            }
            out = new FileOutputStream(filePathNew);
            // 删除模板页
            xwb.removeSheetAt(1);
            xwb.write(out);
            xwb.close();
            // xwbTemp.close();
            out.close();
            flag = true;
            // fileBO.deleteFile(fileNameNewTemp);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        log.debug("写入完成!");
        return filePathNew;
    }

    public static void setCellValue(XSSFSheet xSheet, int row, int col, String value) {
        try {
            xSheet.getRow(row).getCell(col).setCellValue(value);
        } catch (Exception e) {
            log.debug("生成文件时出错:行" + row + "列" + col);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void setCellValue(XSSFSheet xSheet, int row, int col, int value) {
        try {
            xSheet.getRow(row).getCell(col).setCellValue(value);
        } catch (Exception e) {
            log.debug("生成文件时出错:行" + row + "列" + col);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void setCellValue(XSSFSheet xSheet, int row, int col, double value) {
        try {
            xSheet.getRow(row).getCell(col).setCellValue(value);
        } catch (Exception e) {
            log.debug("生成文件时出错:行" + row + "列" + col);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static String getCellValue(XSSFSheet xSheet, int row, int col) {
        try {
            return xSheet.getRow(row).getCell(col).getStringCellValue();
        } catch (Exception e) {
            log.debug("读取文件时出错:行" + row + "列" + col);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
}
